import requests
import consul
header = {
    "Content-Type": "application/json"
}


def register(name, id, address, port):
    url = "http://192.168.178.120:8500/v1/agent/service/register"
    rsp = requests.put(url, headers=header, json={
        "Name": name,
        "ID": id,
        "Tags": ["mxshop", "bobby", "imooc", "chx"],
        "Address": address,
        "Port": port,
        "Check": {
            # "HTTP": f"http://{address}:{port}/health",
            "GRPC": f"{address}:{port}",
            "GRPCUseTLS": False,
            "Timeout": "5s",
            "Interval": "5s",
            "DeregisterCriticalServiceAfter": "5s",
        }
    })
    if rsp.status_code == 200:
        print("注册成功")
    else:
        print(f"注册失败{rsp.status_code}")


def deregister(id):
    url = f"http://192.168.178.120:8500/v1/agent/service/deregister/{id}"
    rsp = requests.put(url, headers=header)
    if rsp.status_code == 200:
        print("注销成功")
    else:
        print("注销失败")
        print("lala")
def mylala():
    c = consul.Consul(host="192.168.178.120")

    address ="192.168.178.1"
    port = 50051
    check = {
        "GRPC": f"{address}:{port}",
        "GRPCUseTLS": False,
        "Timeout": "5s",
        "Interval": "5s",
        "DeregisterCriticalServiceAfter": "15s"
    }
    rsp = c.agent.service.register(name="user-srv",service_id="user-srv",address=address,port=port,
                                   tags=["ddd"],check=check)
    print(rsp)
if __name__ == '__main__':
    # register("mshop-web", "mshop-web", "192.168.178.1", 50051)
    deregister("user-srv")
    # mylala()